package com.exam.controller;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.exam.entity.ApiResult;
import com.exam.entity.ExamManage;
import com.exam.entity.Student;
import com.exam.serviceimpl.ExamManageServiceImpl;
import com.exam.util.ApiResultHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping(value = {"/examManagecontroller"})
//@RestController
public class ExamManageController {

    @Autowired
    private ExamManageServiceImpl examManageService;

    @RequestMapping(value = "/findAll1")
    public ResponseEntity<List<ExamManage>> findAll(){
        return new ResponseEntity<List<ExamManage>>(examManageService.findAll(), HttpStatus.OK);
    }


    @RequestMapping(value = "/findAll2/{page}/{size}")
    public ResponseEntity<Page<ExamManage>> findAll(@PathVariable("page") Integer page, @PathVariable("size") Integer size){
        System.out.println("分页查询所有试卷");
        List<ExamManage> examManageList=  this.examManageService.findAll(page, size);
//        Page<ExamManage> examManage = new Page<>(page,size);
//        IPage<ExamManage> all = examManageService.findAll(examManage);
        return  new ResponseEntity<Page<ExamManage>>((Page<ExamManage>) examManageList,HttpStatus.OK);
    }



    @RequestMapping("/findById/{examCode}")
    public ResponseEntity<ExamManage> findById(@PathVariable("examCode") Integer examCode){
        System.out.println("根据ID查找");
      return  new ResponseEntity<ExamManage>(this.examManageService.findById(examCode),HttpStatus.OK);

    }
    @RequestMapping("/deleteById/{examCode}")
    public ResponseEntity<Integer> deleteById(@PathVariable("examCode") Integer examCode){
        int res = examManageService.delete(examCode);
       return  new ResponseEntity<Integer>(res,HttpStatus.OK);
    }

    @PostMapping("/update")
    public ResponseEntity<Integer> update(@RequestBody ExamManage exammanage){
        int res = examManageService.update(exammanage);
//        if (res == 0) {
//            return ApiResultHandler.buildApiResult(20000,"请求参数错误");
//        }
        System.out.print("更新操作执行---");
        return new ResponseEntity<Integer>(res,HttpStatus.OK);
    }

    @RequestMapping("/add")
    public ResponseEntity<Integer> add(@RequestBody ExamManage exammanage){
        int res = examManageService.add(exammanage);
        return  new ResponseEntity<Integer>(res,HttpStatus.OK);
    }

    @RequestMapping("/examManagePaperId")
    public ResponseEntity<ExamManage> findOnlyPaperId() {
        ExamManage res = examManageService.findOnlyPaperId();
        return  new ResponseEntity<ExamManage>(res,HttpStatus.OK);
    }
}
